package com.talent.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.talent.web.enums.ResponseEnum;
import com.talent.web.exception.TalentException;
import com.talent.web.mapper.SysRoleMapper;
import com.talent.web.model.convertor.RoleConvertor;
import com.talent.web.model.dto.AllocationPermissionDto;
import com.talent.web.model.dto.SysRoleDto;
import com.talent.web.model.entity.SysRole;
import com.talent.web.model.query.RoleQuery;
import com.talent.web.model.vo.OptionVo;
import com.talent.web.service.ISysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName：SysRoleServiceImpl
 * @Author: hszhou
 * @Date: 2024/10/22 9:06
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Override
    public void saveRole(SysRoleDto sysRoleDto) {
        SysRole sysRole = RoleConvertor.INSTANCE.dtoToPo(sysRoleDto);
        this.baseMapper.insert(sysRole);
    }

    @Override
    public void deleteRoleById(Long id) {
        this.baseMapper.deleteById(id);
    }

    @Override
    public void updateRole(SysRoleDto dto) {
        Long id = dto.getId();
        SysRole sysRole = this.baseMapper.selectById(id);
        if(null==sysRole){
            throw new TalentException(ResponseEnum.DATA_NOT_FOUND);
        }
        SysRole update = RoleConvertor.INSTANCE.update(dto, sysRole);
        this.baseMapper.updateById(update);
    }

    @Override
    public IPage<SysRole> page(int current, int pageSize, RoleQuery query) {

        IPage<SysRole> page = new Page(current,pageSize);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotEmpty(query.getName())){
            queryWrapper.eq(SysRole::getName,query.getName());
        }
        if(StrUtil.isNotEmpty(query.getCode())){
            queryWrapper.eq(SysRole::getCode,query.getCode());
        }
        return this.page(page,queryWrapper);

    }

    @Override
    public List<OptionVo> option() {

        List<SysRole> list = super.list();
        return list.stream().map(e-> new OptionVo(e.getId().toString(),e.getName())).collect(Collectors.toList());
    }

    @Transactional
    @Override
    public void allocationPermission(AllocationPermissionDto dto) {

        Long roleId = dto.getRoleId();

        //获取角色的已经有的权限
        List<String> hasPermission =  this.baseMapper.getHasPermission(roleId).stream().map(Object::toString).collect(Collectors.toList());

        ArrayList<String> newArrayList = CollectionUtil.newArrayList(hasPermission);
        //设置的权限
        List<String> settingPermission = dto.getPermission();

        //角色有的与设置取差集就是需要删除的
        if(!CollectionUtil.isEmpty(hasPermission)){

            hasPermission.removeAll(settingPermission);
            if(!CollectionUtil.isEmpty(hasPermission)){
                //has就是需要删除的
                this.baseMapper.deleteByRoleIdAndPermissionIds(roleId,hasPermission);
            }


        }

        //设置的与角色有的差集就是需要新增的

         settingPermission.removeAll(newArrayList);
        if(!CollectionUtil.isEmpty(settingPermission)){
            this.baseMapper.saveRolePermission(settingPermission,roleId);
        }


    }

    @Override
    public List<String> hasPermission(Long roleId) {
       return   this.baseMapper.getHasPermission(roleId)
                .stream().map(Object::toString).collect(Collectors.toList());
    }

}
